<template>
	<view>
		<view class="pb15 bgfff" id="sticky-nav">
			<view class="ml25 mr25 search flex flex-ac pl25 pr25">
				<image src="http://tuozhen1.oss-cn-beijing.aliyuncs.com/ueditor/upload/weixin/images/search.png" mode="widthFix"></image>
				<input class="it1" @input="setKeyword" @confirm="search" placeholder="搜医生、科室、症状" />
			</view>
		</view>

		<block v-if="result==null">
			<view class="ml25 mr25 flex flex-ac flex-pj mt30 mb20">
				<text>历史搜索</text>
				<image class="delete" src="http://tuozhen1.oss-cn-beijing.aliyuncs.com/ueditor/upload/weixin/images/delete.png" @click="deleteHistory"></image>
			</view>
			<!-- <view class="history overflow" v-for="(item,index) in resInfoList" :key="index"> -->
			<view class="ml25" style="margin-bottom: 10rpx;color: #868686;" v-for="(item,index) in resInfoList" :key="index" @click="getResInfoList(item)">
				{{item}}
				<!-- <text class="history overflow"  v-for="(item,index) in resInfoList" :key="index" @click="getResInfoList(item)">{{item}}</text> -->
			</view>
				
			<!-- </view> -->
		</block>
		<block v-if="result">
			<block v-if="result.length">
				<view class="pl25 pr25">
					<view class="pt30 pb30 bb ft15 c74a6fd" style="color: #00BAAD;">
						疾病
					</view>
				</view>
				<view class="pl25 pr25" v-for="(item,index) in searchDiseList" :key="index">
					<view class="pt20 pb20" @click="toWikiIndex(item.diseDiseaseId)">{{item.diseDiseaseName}}</view>
				</view>
				<uni-load-more :status="dismessage" :contentText="contentText" @clickLoadMore="loadMores"></uni-load-more>
				<view class="pl25 pr25">
					<view class="pt30 pb30 bb ft15 c74a6fd" style="color: #00BAAD;">
						推荐医生
					</view>
				</view>
				<view class="list pl25 pr25">
					<view class="flex" v-for="(item,index) in searchDocList" :key="index" @click="toDocIndex(item.medDocInfoId,item.recNum)">
						<view class="rela head">
							<image :src="item.medDocInfoPhoto"></image>
							<view class="w100 abs texts ftc cfff" v-if="item.onlineStates==8"><text class="text3" style="background: #00BAAD;">接诊</text></view>
							<view class="w100 abs texts ftc cfff" v-if="item.onlineStates==0"><text class="text3">离线</text></view>
							<view class="w100 abs texts ftc cfff" v-if="item.onlineStates==7"><text class="text3" style="background: #B1E5E1;">休息</text></view>
							<view class="w100 abs texts ftc cfff" v-if="item.onlineStates==6"><text class="text3">离开</text></view>
						</view>
						<view class="it1 pl30">
							<view class="flex flex-ac">
								<text class="ft16 c000 bold">{{item.medDocInfoName}}</text>
								<text class="ml30">{{item.medDocInfoTitle}}</text>
							</view>
							<view class="mt20">{{item.baseDeptName}}</view>
							<view class="lineText mt20">{{item.disease}}</view>
							<view class="mt25 flex flex-ac flex-pc">
								<view class="it1 flex flex-ac doctor">
									<!-- <view class="doctorv" v-if="item.ismessage==1">
										<image src="http://tuozhen1.oss-cn-beijing.aliyuncs.com/ueditor/upload/weixin/images/imgy.png"></image>
										<text>¥{{item.wordPrice}}</text>
									</view>
									<view class="doctorv" v-if="item.ismessage==0">
										<image src="http://tuozhen1.oss-cn-beijing.aliyuncs.com/ueditor/upload/weixin/images/imgn.png"></image>
									</view>
									<view class="doctorv" v-if="item.isvideo==1">
										<image src="http://tuozhen1.oss-cn-beijing.aliyuncs.com/ueditor/upload/weixin/images/videoy.png"></image>
										<text>¥{{item.videoPrice}}</text>
									</view>
									<view class="doctorv" v-if="item.isvideo==0">
										<image src="http://tuozhen1.oss-cn-beijing.aliyuncs.com/ueditor/upload/weixin/images/videon.png"></image>
									</view> -->
									
									<view class="doctorv" v-if="item.ismessage==1" style="display: flex;align-items: center;">
										<image src="../../static/images/tuwen.png"></image>
										<text style="color: #00BAAD;">¥{{' '+item.wordPrice}}</text>
									</view>
									<view class="doctorv" v-if="item.ismessage==0" style="display: flex;align-items: center;">
										<image src="../../static/images/wkttw.png"></image>
										<text style="color: #B1E5E1;">未开通</text>
									</view>
									<view class="doctorv" v-if="item.isvideo==1" style="display: flex;align-items: center;">
										<image src="../../static/images/shipi.png"></image>
										<text style="color: #00BAAD;">¥{{' '+item.videoPrice}}</text>
									</view>
									<view class="doctorv" v-if="item.isvideo==0" style="display: flex;align-items: center;">
										<image src="../../static/images/wktsp.png"></image>
										<text style="color: #B1E5E1;">未开通</text>
									</view>
								</view>
								<text class="ft13 c666">已服务{{item.recNum}}人</text>
							</view>
						</view>
					</view>

				</view>
			</block>
			<block v-else>
				<image class="block ct none" src="http://tuozhen1.oss-cn-beijing.aliyuncs.com/ueditor/upload/weixin/images/none.png"></image>
			</block>
		</block>
		<uni-load-more :status="docmessage" :contentText="contentText" @clickLoadMore="loadMore"></uni-load-more>
		<view style="text-align: center;font-size: 10px;" v-if="statusOne">由重庆拓诊信息技术有限公司提供技术支持</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				statusOne:false,
				usrUserId: null,
				wx: "https://app.mzmzb.com", //https://app.mzmzb.com   http://localhost:6110
				keyword: '',
				result: null,
				resInfo: '', //缓存
				current: 1,
				searchDocList: [], //搜索医生列表
				searchDiseList: [], //搜索疾病列表
				docmessage: 'noMore',
				dismessage: 'noMore',
				contentText: {
					contentdown: "点击显示更多",
					contentrefresh: "正在加载...",
					contentnomore: "暂无数据"
				},
				dochasNextPage: true, //医生是否有下一页
				dishasNextPage: true, //疾病是否有下一页
				resInfoList: [], //缓存列表
			};
		},
		onLoad(option) {
			this.usrUserId = option.usrUserId;
			this.getStorageInfo();
		},
		onShow() {

		},
		onReachBottom(){
			this.statusOne=true
		},
		methods: {
			//加载更多
			loadMore(e) {
				var that = this;
				if (e.detail.status == 'more') {
					that.docmessage = 'loading';
					that.current = that.current + 1;
					console.log("that.current:" + that.current)
					setTimeout(() => {
						that.search(that.keyword)
						if (that.dochasNextPage) {
							that.docmessage = 'more';
						} else {
							that.docmessage = 'noMore';
						}
						// if(that.dishasNextPage){
						// 	that.dismessage = 'more';
						// }else{
						// 	that.dismessage = 'noMore';
						// }
					}, 500);
				}
			},
			loadMores(e) {
				var that = this;
				if (e.detail.status == 'more') {
					that.dismessage = 'loading';
					that.current = that.current + 1;
					console.log("that.current:" + that.current)
					setTimeout(() => {
						that.search(that.keyword)
						// if(that.dochasNextPage){
						// 	that.docmessage = 'more';
						// }else{
						// 	that.docmessage = 'noMore';
						// }
						if (that.dishasNextPage) {
							that.dismessage = 'more';
						} else {
							that.dismessage = 'noMore';
						}
					}, 500);
				}
			},
			getStorageInfo() {
				var that = this;
				uni.getStorage({
					key: "info",
					success: function(e) {
						console.log("缓存" + e.data)
						that.resInfoList = e.data;
					}
				})
			},
			setKeyword(e) {
				var that = this
				this.keyword = e.detail.value
				console.log(this.keyword)

			},
			search(e) {
				var that = this
				if(that.keyword==''){
					uni.showLoading({
						title:'请输入搜索的内容'
					})
					setTimeout(() => {
						uni.hideLoading()
					}, 1000);
					return
				}
				var key = uni.getStorageSync("keyword")
				console.log("key" + key + ":e:" + that.keyword)
				if (that.keyword != key) {
					that.searchDocList = []
					that.searchDiseList = []
					that.current = 1
				}
				console.log("搜索:" + that.current)
				that.result = [1, 2]
				uni.setStorageSync("keyword", that.keyword)
				let data={
					searchType: "ALL",
					keyWord: that.keyword,
					hosId: this.hspId,
					current: that.current
				}
				this.func.$request('wx/searchInfo.tz',data,'GET').then(res=>{
					if (res.data.success = true) {
						console.log(res.data)
						that.dochasNextPage = res.data.result.searchDocResult.hasNextPage;
						if (that.dochasNextPage) {
							that.docmessage = 'more';
						} else {
							that.docmessage = 'noMore';
						}
					
						that.dishasNextPage = res.data.result.searchDiseResult.hasNextPage;
						if (that.dishasNextPage) {
							that.dismessage = 'more';
						} else {
							that.dismessage = 'noMore';
						}
						var searchDocResult = [];
						searchDocResult = res.data.result.searchDocResult.list
						if (searchDocResult.length > 0) {
							searchDocResult.forEach(function(item, index) {
								that.searchDocList.push(item);
							});
							var uniques = [];
							var stringify = {};
							for (var i = 0; i < that.searchDocList.length; i++) {
								var keys = Object.keys(that.searchDocList[i]);
								keys.sort(function(a, b) {
									return (Number(a) - Number(b));
								});
								var str = '';
								for (var j = 0; j < keys.length; j++) {
									str += JSON.stringify(keys[j]);
									str += JSON.stringify(that.searchDocList[i][keys[j]]);
								}
								if (!stringify.hasOwnProperty(str)) {
									uniques.push(that.searchDocList[i]);
									stringify[str] = true;
								}
							}
							that.searchDocList = uniques
						}
						var searchDiseResult = [];
						searchDiseResult = res.data.result.searchDiseResult.list
						if (searchDiseResult.length > 0) {
							searchDiseResult.forEach(function(item, index) {
								that.searchDiseList.push(item);
							});
							var uniques = [];
							var stringify = {};
							for (var i = 0; i < that.searchDiseList.length; i++) {
								var keys = Object.keys(that.searchDiseList[i]);
								keys.sort(function(a, b) {
									return (Number(a) - Number(b));
								});
								var str = '';
								for (var j = 0; j < keys.length; j++) {
									str += JSON.stringify(keys[j]);
									str += JSON.stringify(that.searchDiseList[i][keys[j]]);
								}
								if (!stringify.hasOwnProperty(str)) {
									uniques.push(that.searchDiseList[i]);
									stringify[str] = true;
								}
							}
							that.searchDiseList = uniques
						}
						that.resInfoList.push(that.keyword);
						//去重
						var hash = [];
						for (var i = 0; i < that.resInfoList.length; i++) {
							if (hash.indexOf(that.resInfoList[i]) == -1) {
								hash.push(that.resInfoList[i]);
							}
						}
						uni.setStorage({
							key: 'info',
							data: hash,
						})
					} else {
						that.docmessage = 'noMore';
						that.dismessage = 'noMore';
					
					}
				})
				
			},
			//点击历史记录查询
			getResInfoList(e) {
				var that = this
				that.keyword = e
				that.result = [1, 2]
				uni.setStorageSync("keyword", e)
				let data={
					searchType: "ALL",
					keyWord: e,
					hosId: this.hspId,
					current: that.current
				}
				this.func.$request('wx/searchInfo.tz',data,'GET').then(res=>{
					if (res.data.success = true) {
						that.dochasNextPage = res.data.result.searchDocResult.hasNextPage;
						if (that.dochasNextPage) {
							that.docmessage = 'more';
						} else {
							that.docmessage = 'noMore';
						}
					
						that.dishasNextPage = res.data.result.searchDiseResult.hasNextPage;
						if (that.dishasNextPage) {
							that.dismessage = 'more';
						} else {
							that.dismessage = 'noMore';
						}
						console.log(res.data.result.searchDocResult.list)
						console.log(res.data.result.searchDiseResult.list);
						var searchDocResult = [];
						var searchDiseResult = [];
						searchDocResult = res.data.result.searchDocResult.list
						searchDiseResult = res.data.result.searchDiseResult.list
						if (searchDocResult != null) {
							that.searchDocList = res.data.result.searchDocResult.list
						}
						if (searchDiseResult != null) {
							that.searchDiseList = res.data.result.searchDiseResult.list
						}
						// uni.setStorage({
						// 	key: 'info',
						// 	data: that.keyword,
						// })
					}
				})
			},
			//跳转医生首页
			toDocIndex(id, recNum) {
				var that = this;
				var docId = id;
				console.log("用户ID" + that.usrUserId)
				console.log("医生ID" + docId)
				uni.navigateTo({
					url: '/pages4/doctor/index?docId=' + docId + '&usrUserId=' + that.usrUserId + '&recNum=' + recNum
				});
			},
			//跳转病情百科首页
			toWikiIndex(id) {
				var that = this;
				var disId = id;
				uni.navigateTo({
					url: '/pages/wiki/index?diseDiseaseId=' + disId
				});
			},
			deleteHistory() {
				console.log("123")
				uni.removeStorageSync('info');
				this.resInfoList = [];
			}
		}
	};
</script>

<style lang="less">
	#sticky-nav {
		position: -webkit-sticky;
		position: sticky;
		top: 0;
		left: 0;
		z-index: 10;
	}

	.search {
		height: 65rpx;
		background: #efefef;
		border-radius: 10rpx;

		image {
			width: 32rpx;
			height: 32rpx;
			margin-right: 10rpx;
		}
	}

	.delete {
		width: 32rpx;
		height: 32rpx;
	}

	.history {
		padding: 0 13rpx;

		text {
			display: inline-block;
			line-height: 50rpx;
			border-radius: 25rpx;
			padding: 0 30rpx;
			background: #EFEFEF;
			color: #b6b9bf;
			margin: 12rpx;
		}
	}

	.list>view {
		padding: 40rpx 0;
		border-top: 1rpx solid #efefef;

		.doctor {
			color: #74a6fd;

			.doctorv {
				width: 150rpx;

				image {
					width: 32rpx;
					height: 32rpx;
					margin-right: 10rpx;
				}
			}
		}
	}

	.list .head {
		height: 140rpx;

		image {
			width: 140rpx;
			height: 140rpx;
			border-radius: 50%;
		}

		.texts {
			height: 32rpx;
			bottom: -4rpx;

			text {
				display: inline-block;
				height: 32rpx;
				line-height: 32rpx;
				border-radius: 16rpx;
				background: #74a6fd;
				font-size: 22rpx;
				padding: 0 15rpx;
			}

			.text1 {
				background: #ff0000;
			}
			
			.text3 {
				background: #999999;
			}
			
			.text2 {
				background: #fec322;
			}
		}
	}

	.none {
		margin-top: 30%;
		width: 485rpx;
		height: 385rpx;
	}
</style>
